package com.huanying.goodjob.community;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;

@Service
public class CommunityServiceImpl implements CommunityService{

	@Autowired
	private BaseDao dao;
	
	private static Logger logger = Logger.getLogger(CommunityServiceImpl.class);
	
	@Override
	public void add(Community community) throws Exception {
		
		dao.save(community);
	}
	
	@Override
	public void update(Community community) throws Exception {
		dao.update(community);
		
	}

	@Override
	public void delete(Community community) throws Exception {
		dao.delete(community);
	}
	
	
	@Override
	public Community getbyId(int id) throws Exception {
		return (Community) dao.load(Community.class, id);
	}

	@Override
	public Area getbyCode(String code) throws Exception{
		Area area = new Area();
		String hql = "from Area as a where a.code=?";
		List<Object> params = new ArrayList<Object>();
		params.add(code);
		area=(Area) dao.exculeHqlByParams(hql, params).get(0);
		return area;
	}

	@Override
	public List getbyCountyCode(int county_code) throws Exception{
		List<Area> list = new ArrayList<Area>();
		String hql = "from Community c where c.county.id =? and c.status=1";
		List<Object> params = new ArrayList<Object>();
		params.add(county_code);
		list=dao.exculeHqlByParams(hql, params);
		return list;
	}

	
	@Override
	public List getProvince() throws Exception {
		List<Area> list = new ArrayList<Area>();
		String hql = "from Area a where a.status=1 and a.level=1";
        Session session=null;
		session =dao.getSession();
		Query query = session.createQuery(hql);
		return query.list();
	}

	@Override
	public List getCity(String code) throws Exception {
		List<Area> list = new ArrayList<Area>();
		String hql = "from Area a where a.status=1 and a.level=2 and a.previous_code=?";
		List<Object> params = new ArrayList<Object>();
		params.add(code);
		list=dao.exculeHqlByParams(hql, params);
		logger.info(list.get(0).getCode());
		return list;
	}
	@Override
	public List getCounty(String code) throws Exception {
		List<Area> list = new ArrayList<Area>();
		String hql = "from Area a where a.status=1 and a.level=3 and a.previous_code=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(code);
		list=dao.exculeHqlByParams(hql, params);
		return list;
	}
	
		public PageBean searchCommunity(Map<String, String> map, int pageSize, int page) throws Exception {
			// TODO Auto-generated method stub
			StringBuilder hql = new StringBuilder("from Community as c where 1=1");
			List<Object> param = new ArrayList<Object>();
			if (map.get("province") != "" && map.get("province") != null) {
				hql.append(" and c.province.code=?");
				param.add(map.get("province"));
			}
			if (map.get("city") != "" && map.get("city") != null) {
				hql.append(" and c.city.code=?");
				param.add(map.get("city"));
			}

			if (map.get("county") != null && map.get("county") != "") {
				hql.append(" and c.county.code=?");
				param.add(map.get("county"));
			}
			if (map.get("xqname") != null && map.get("xqname") != "") {
				hql.append(" and c.name like ?");
				param.add("%" + map.get("xqname") + "%");
			}	
			if(map.get("status")!=null && map.get("status")!="")
			{
				hql.append(" and c.status=?");
				param.add(Integer.parseInt(map.get("status")));
			}
			int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}
		@Override
		public int delete_community(int id) throws Exception {

			List<Object> param = new ArrayList<Object>();
			String hql = "update Community set status=0 where id=?";
			param.add(id);
			return dao.exculeByParams(hql, param);
		}

		@Override
		public String getCounty_bystring(String county) throws Exception {
			StringBuilder hql = new StringBuilder("from Area as a where 1=1");
			List<Object> param = new ArrayList<Object>();
			Area area = new Area();
			if(county != null && !("").equals(county)){
				hql.append(" and a.name = ?");
				param.add(county);
			}
			List<Object> list = dao.exculeHqlByParams(hql.toString(), param);
			if(list != null){
				area = (Area) list.get(0);
			}
			return area.getCode();
		}
}
